﻿using System.Web.Mvc;
using FrameWork.Common.Const;
using FrameWork.Context.Oprator;
using FrameWork.Util.Cookie;

namespace FrameWork.Web.Attribute
{
    public class HandlerLoginAttribute : AuthorizeAttribute
    {
        /// <summary>
        /// 是否忽略，不验证是否登录过期
        /// </summary>
        private bool Ignore;

        /// <summary>
        /// 构造方法，接收传入的参数
        /// </summary>
        /// <param name="ignore">false：不验证登录，true：需要验证登录</param>
        public HandlerLoginAttribute(bool ignore = true)
        {
            Ignore = ignore;
        }

        /// <summary>
        /// 登陆时对机构进行验证
        /// </summary>
        /// <param name="filterContext">返回登陆页面</param>
        public override void OnAuthorization(AuthorizationContext filterContext)
        {
            if (Ignore == false)
            { 
                return;
            }
            var operatorProvider = OperatorProvider.Provider.GetCurrent();
            if (operatorProvider == null)
            {
                Cookie.SaveWithMinute(CookieConst.CookieLoginKey, CookieConst.CookieLoginOverDue, CookieConst.CookieLoginOverMinutes);
                filterContext.Result = new RedirectResult(CookieConst.CookieLoginDefaultUrl);
            }
        }
    }
}